import request from '@/utils/request'
import moment from 'moment'
export default {
  components: {},
  props: ['type'],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        brand: undefined,
        campusId: undefined,
        policyName: undefined,
        policyNumber: undefined,
        effectiveDate: undefined,
        expirationDate: undefined,
        insuranceCompany: undefined,
        createTime: undefined,
        createUserName: undefined,
        description: undefined,
        isCurrent: true
      },
      brandOptions: [],
      campusOptions: [],
      campusLoading: false,
      policyNameOptions: [
        {
          enCode: '1',
          fullName: '三合一'
        },
        {
          enCode: '2',
          fullName: '雇主责任险'
        },
        {
          enCode: '3',
          fullName: '公众责任险'
        },
        {
          enCode: '4',
          fullName: '财产一切险'
        },
        {
          enCode: '5',
          fullName: '公众责任险+雇主责任险'
        },
        {
          enCode: '6',
          fullName: '公众责任险+财产一切险'
        },
        {
          enCode: '7',
          fullName: '其他'
        }
      ],
      insuranceCompanyOptions: [
        // {
        //   enCode: '1',
        //   fullName: '未办理'
        // },
        {
          enCode: '2',
          fullName: '安亲保'
        },
        {
          enCode: '3',
          fullName: '放心保'
        },
        {
          enCode: '4',
          fullName: '平安保险'
        },
        {
          enCode: '5',
          fullName: '太平洋保险'
        },
        {
          enCode: '6',
          fullName: '中国人寿'
        },
        {
          enCode: '7',
          fullName: '阳光保险'
        },
        {
          enCode: '8',
          fullName: '其他'
        }
      ],
      rules: {
        campusId: [
          {
            required: true,
            message: '请选择投保校区',
            trigger: 'change'
          }
        ],
        policyName: [
          {
            required: true,
            message: '请输入保险名称',
            trigger: 'blur'
          }
        ],
        policyNumber: [
          {
            required: true,
            message: '请输入投保单号',
            trigger: 'blur'
          }
        ],
        effectiveDate: [
          {
            required: true,
            message: '请选择生效日期',
            trigger: 'change'
          }
        ],
        expirationDate: [
          {
            required: true,
            message: '请选择到期日期',
            trigger: 'change'
          }
        ],
        insuranceCompany: [
          {
            required: true,
            message: '请输入投保公司',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    'dataForm.effectiveDate': {
      handler(newVal) {
        if (newVal) {
          this.dataForm.expirationDate = moment(newVal).add(1, 'years')
        } else {
          this.dataForm.expirationDate = undefined
        }
      },
      deep: true
    },
    'dataForm.brand': {
      async handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          try {
            this.campusLoading = true
            await getCampusSelector({ brand: newVal, status: 'normal', fields: 'id,fullName,siteArea' }).then(res => {
              this.campusOptions = Array.isArray(res.data) ? res.data : []
            })
          } catch (err) {
            console.error('加载校区下拉选项失败:', err)
            this.$message.error('校区选项加载失败')
            this.campusOptions = []
          } finally {
            this.campusLoading = false
          }
        }
      }
    }
  },
  created() {},
  mounted() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail, campusId) {
      this.dataForm.id = id || 0
      this.dataForm.campusId = campusId
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/campus/insurance/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    dataFormSubmit() {
      this.loading = true
      // 防连点
      this.searchTimer && clearTimeout(this.searchTimer)
      this.searchTimer = setTimeout(() => {
        this.$refs['elForm'].validate(valid => {
          if (valid) {
            if (!this.dataForm.id) {
              request({
                url: `/api/campus/insurance`,
                method: 'post',
                data: this.dataForm
              }).then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.loading = false
                    this.visible = false
                    this.$emit('refresh', true)
                  }
                })
              })
            } else {
              request({
                url: '/api/campus/insurance/' + this.dataForm.id,
                method: 'PUT',
                data: this.dataForm
              }).then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.loading = false
                    this.visible = false
                    this.$emit('refresh', true)
                  }
                })
              })
            }
          }
          this.loading = false
        })
      }, 300)
    }
  }
}
